/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ventanaPagarTarjeta.java
 *
 * Created on 25/06/2010, 12:12:16 PM
 */

package CapaGrafica;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.DocumentException;
import CapaLogica.ComunicaReserva;
import CapaLogica.ComunicaPago;
import CapaDatos.GenerarFactura;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
 *
 * @author Maria
 */
public class ventanaPagarTarjeta extends javax.swing.JFrame {

    /** Creates new form ventanaPagarTarjeta */
    public ventanaPagarTarjeta() {
        initComponents();
        this.setSize(500,500);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTFMonto = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jTFBanco = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTFNumTarjeta = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        JChbCredito = new javax.swing.JCheckBox();
        JChbDebito = new javax.swing.JCheckBox();
        jBProcesar = new javax.swing.JButton();
        jBCancelar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Pago con tarjeta");
        setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        setLocationByPlatform(true);

        jTFMonto.setName("jTFMonto"); // NOI18N

        jLabel1.setText("Monto:");
        jLabel1.setName("jLabel1"); // NOI18N

        jTFBanco.setName("jTFBanco"); // NOI18N

        jLabel2.setText("Banco:");
        jLabel2.setName("jLabel2"); // NOI18N

        jTFNumTarjeta.setName("jTFNumTarjeta"); // NOI18N
        jTFNumTarjeta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFNumTarjetaActionPerformed(evt);
            }
        });

        jLabel3.setText("Numero de Tarjeta:");
        jLabel3.setName("jLabel3"); // NOI18N

        JChbCredito.setText("Credito");
        JChbCredito.setName("JChbCredito"); // NOI18N
        JChbCredito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JChbCreditoActionPerformed(evt);
            }
        });

        JChbDebito.setText("Debito");
        JChbDebito.setName("JChbDebito"); // NOI18N
        JChbDebito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JChbDebitoActionPerformed(evt);
            }
        });

        jBProcesar.setText("Procesar");
        jBProcesar.setName("jBProcesar"); // NOI18N
        jBProcesar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBProcesarActionPerformed(evt);
            }
        });

        jBCancelar.setText("Cancelar");
        jBCancelar.setName("jBCancelar"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel5.setFont(new java.awt.Font("Trebuchet MS", 0, 36));
        jLabel5.setForeground(new java.awt.Color(0, 51, 204));
        jLabel5.setText("i-Resort.");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel4.setText("Su pago:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/CapaGrafica/mastercard.jpg"))); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/CapaGrafica/visa.png"))); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(14, 14, 14)
                                .addComponent(JChbCredito)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(JChbDebito)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTFNumTarjeta)
                                .addComponent(jTFBanco)
                                .addGroup(layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jTFMonto, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap(117, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(150, 150, 150)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 164, Short.MAX_VALUE)
                        .addComponent(jLabel7)
                        .addGap(21, 21, 21))))
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addComponent(jBProcesar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 240, Short.MAX_VALUE)
                .addComponent(jBCancelar)
                .addGap(61, 61, 61))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTFMonto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTFBanco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTFNumTarjeta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(41, 41, 41)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(JChbCredito)
                            .addComponent(JChbDebito))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBProcesar)
                    .addComponent(jBCancelar))
                .addGap(48, 48, 48))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
private double montoTarjeta;
boolean bandera1=false,bandera2=false;
    private void jBProcesarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBProcesarActionPerformed
      ComunicaReserva comunicameAReserva = new ComunicaReserva();
ComunicaPago comunicameAPago = new ComunicaPago();
boolean datosCompletos = comunicameAPago.validarDatosTarjeta(jTFNumTarjeta,jTFBanco, jTFMonto);
double montoTotal = getMontoTarjeta();
if (datosCompletos && bandera1 == false)
{
    double montoIngresado = Double.parseDouble(jTFMonto.getText());
  if(comunicameAPago.esPagoCompletoTarjeta(jTFNumTarjeta,montoIngresado, montoTotal,jTFBanco,JChbCredito.getText()))
     {
         JOptionPane.showMessageDialog(rootPane, "Enhorabuena el pago de su reserva se ha realizado satisfactoriamente");
         GenerarFactura laFactura = new GenerarFactura();
                try {
                    laFactura.recibirReserva(comunicameAReserva.getLaReserva(), ComunicaPago.getLosPagos());
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ventanaPagarTarjeta.class.getName()).log(Level.SEVERE, null, ex);
                } catch (BadElementException ex) {
                    Logger.getLogger(ventanaPagarTarjeta.class.getName()).log(Level.SEVERE, null, ex);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(ventanaPagarTarjeta.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(ventanaPagarTarjeta.class.getName()).log(Level.SEVERE, null, ex);
                } catch (DocumentException ex) {
                    Logger.getLogger(ventanaPagarTarjeta.class.getName()).log(Level.SEVERE, null, ex);
                }
         this.dispose();
     }
   else
      this.dispose();
}
else
    if (datosCompletos && bandera1 == true)
{
    double montoIngresado = Double.parseDouble(jTFMonto.getText());
  if(comunicameAPago.esPagoCompletoTarjeta(jTFNumTarjeta,montoIngresado, montoTotal,jTFBanco,JChbDebito.getText()))
     {
         JOptionPane.showMessageDialog(rootPane, "Enhorabuena el pago de su reserva se ha realizado satisfactoriamente");
         GenerarFactura laFactura = new GenerarFactura();
                try {
                    laFactura.recibirReserva(comunicameAReserva.getLaReserva(), ComunicaPago.getLosPagos());
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ventanaPagarTarjeta.class.getName()).log(Level.SEVERE, null, ex);
                } catch (BadElementException ex) {
                    Logger.getLogger(ventanaPagarTarjeta.class.getName()).log(Level.SEVERE, null, ex);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(ventanaPagarTarjeta.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(ventanaPagarTarjeta.class.getName()).log(Level.SEVERE, null, ex);
                } catch (DocumentException ex) {
                    Logger.getLogger(ventanaPagarTarjeta.class.getName()).log(Level.SEVERE, null, ex);
                }
         this.dispose();
     }
   else
      this.dispose();
}
    else
    JOptionPane.showMessageDialog(rootPane, "Falta algun dato, por favor verifique e intente de nuevo"); // TODO add your handling code here:
    }//GEN-LAST:event_jBProcesarActionPerformed

    private void JChbDebitoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JChbDebitoActionPerformed
     if (bandera1==false)
     {
       JChbCredito.setEnabled(false);
       bandera1=true;
       jTFBanco.setEditable(true);
       jTFMonto.setEditable(true);
       jTFNumTarjeta.setEditable(true);
     }
     else
     {
       JChbCredito.setEnabled(true);
       bandera1=false;
     }  // TODO add your handling code here:
    }//GEN-LAST:event_JChbDebitoActionPerformed

    private void JChbCreditoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JChbCreditoActionPerformed
        if (bandera2==false)
       {
         JChbDebito.setEnabled(false);
         bandera2=true;
       jTFBanco.setEditable(true);
       jTFMonto.setEditable(true);
       jTFNumTarjeta.setEditable(true);
       }
       else
       {
         JChbDebito.setEnabled(true);
         bandera2=false;
       }// TODO add your handling code here:
    }//GEN-LAST:event_JChbCreditoActionPerformed

    private void jTFNumTarjetaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFNumTarjetaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTFNumTarjetaActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ventanaPagarTarjeta().setVisible(true);
            }
        });
    }
    public void setMontoTarjeta(double montoTarjeta) {
        this.montoTarjeta = montoTarjeta;
    }
    public double getMontoTarjeta() {
	        return montoTarjeta;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox JChbCredito;
    private javax.swing.JCheckBox JChbDebito;
    private javax.swing.JButton jBCancelar;
    private javax.swing.JButton jBProcesar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTFBanco;
    private javax.swing.JTextField jTFMonto;
    private javax.swing.JTextField jTFNumTarjeta;
    // End of variables declaration//GEN-END:variables

}
